﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.FrontPageShow
{
    public partial class AddMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtMessageText.Text = "";
            this.txtVisitorName.Text = "";
            this.txtVisitorContact.Text = "";
            this.txtCode.Text = "";
            this.lbCode.Text = "";
            this.lblResult.Text = "";
        }

        protected void btnPutIn_Click(object sender, EventArgs e)
        {
            if (this.txtMessageText.Text.Trim() == "")
            {
                this.lblResult.Text = "内容不能为空";
                this.txtMessageText.Focus();
            }
            else if (this.txtMessageText.Text.Length > 200)
            {
                this.lblResult.Text = "内容不能超过200字";
                this.txtMessageText.Focus();
            }
            else
            {
                this.lblResult.Text = "";
                if (this.txtCode.Text.Trim() == "")
                {
                    this.lbCode.Text = "请输入验证码";
                    this.txtCode.Focus();
                }
                else if (this.txtCode.Text.Trim().ToLower() == Request.Cookies["CheckCode"].Value.ToString().ToLower())
                {
                    this.lbCode.Text = "";
                    string name = (this.txtVisitorName.Text.Trim() == "") ? "匿名" : txtVisitorName.Text.Trim();
                    string contact = (this.txtVisitorContact.Text.Trim() == "") ? "无" : txtVisitorContact.Text.Trim();
                    string content = this.txtMessageText.Text;
                    content = txtMessageText.Text.Trim();
                    bool addMessage = CRM.Business.Messages.AddVisitorMessage(name, contact, content);
                    if (!addMessage)
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('留言失败，请重新操作……');</script>");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('留言成功,等待审核……'); location.href='/FrontPageShow/MessagesShow.aspx';</script>");
                       
                    }
                }
                else
                {
                    this.lbCode.Text = "验证码输入有误";
                    this.txtCode.Text = "";
                    this.txtCode.Focus();
                }
            }
        }
    }
}